package com.carl.zlds.service.impl;

import com.carl.zlds.tool.common.PageBO;
import com.carl.zlds.tool.util.AssertUtil;
import com.carl.zlds.tool.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carl.zlds.dao.CustomerServiceMapper;
import com.carl.zlds.model.CustomerService;
import com.carl.zlds.model.Game;
import com.carl.zlds.pojo.dto.CustomerServiceSearchDTO;
import com.carl.zlds.pojo.dto.SaveCustomerServiceDTO;
import com.carl.zlds.pojo.vo.CustomerServiceVO;
import com.carl.zlds.service.ICustomerServiceService;
import com.carl.zlds.service.IGameService;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author carl
 */
@Slf4j
@Service
public class CustomerServiceServiceImpl extends ServiceImpl<CustomerServiceMapper, CustomerService> implements ICustomerServiceService {

    private final IGameService gameService;

    public CustomerServiceServiceImpl(IGameService gameService) {
        this.gameService = gameService;
    }

    @Override
    public void saveCustomerService(Long id, SaveCustomerServiceDTO dto) {
        CustomerService customerService;
        String account = dto.getAccount();
        Long gameId = dto.getGameId();

        if (gameId > 0) {
            Game game = gameService.getOne(new QueryWrapper<Game>().eq("id", gameId).eq("weight", 0));
            AssertUtil.isNull("游戏不存在", game);
        }

        if (id > 0) {
            customerService = this.baseMapper.selectOne(new QueryWrapper<CustomerService>().eq("id", id).eq("weight", 0));
            AssertUtil.isNull("客服不存在", customerService);
            Wrapper<CustomerService> wrapper = new QueryWrapper<CustomerService>().eq("account", account)
                    .eq("weight", 0).ne("id", id);
            CustomerService cs = this.baseMapper.selectOne(wrapper);
            AssertUtil.isNotNull("客服已存在", cs);
        } else {
            Wrapper<CustomerService> wrapper = new QueryWrapper<CustomerService>().eq("id", id)
                    .eq("account", account).eq("weight", 0);
            CustomerService cs = this.baseMapper.selectOne(wrapper);
            AssertUtil.isNotNull("客服已存在", cs);
            customerService = new CustomerService();
        }

        customerService.setAccount(account);
        customerService.setRemark(dto.getRemark());
        customerService.setGameId(gameId);

        if (id > 0) {
            this.baseMapper.updateById(customerService);
        } else {
            this.baseMapper.insert(customerService);
        }
    }

    @Override
    public void deleteCustomerService(Long id) {
        Wrapper<CustomerService> wrapper = new QueryWrapper<CustomerService>().eq("id", id).eq("weight", 0);
        CustomerService cs = this.baseMapper.selectOne(wrapper);
        AssertUtil.isNull("客服不存在", cs);

        cs.setWeight(-1);
        this.baseMapper.updateById(cs);
    }

    @Override
    public Map<String, Object> getCustomerServiceList(CustomerServiceSearchDTO dto, PageBO pageBO) {
        String limitSql = " limit " + (pageBO.getPage() - 1) * pageBO.getLimit() + ", " + pageBO.getLimit();
        List<CustomerServiceVO> list = this.baseMapper.getCustomerServiceList(dto, limitSql);

        for (CustomerServiceVO item : list) {
            Long id = item.getGameId();
            if (id > 0) {
                Game game = gameService.getById(id);
                if (game != null) {
                    item.setGameName(game.getName());
                }
            } else {
                item.setGameName("全部");
            }
        }
        Integer count = this.baseMapper.countCustomerServiceList(dto, "");

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", list);
        map.put("count", count);

        return map;
    }

    @Override
    public CustomerServiceVO searchCustomerService(CustomerServiceSearchDTO dto) {
        String account = dto.getAccount();
        Long gameId = dto.getGameId();
        QueryWrapper<CustomerService> wrapper = new QueryWrapper<CustomerService>().eq("account", account).eq("weight", 0);
        if (gameId != null && gameId > 0) {
            wrapper.eq("game_id", gameId);
        }
        CustomerService cs = this.baseMapper.selectOne(wrapper);
        if (cs != null) {
            CustomerServiceVO customerServiceVO = new CustomerServiceVO();
            ObjUtil.copy(cs, customerServiceVO);
            return customerServiceVO;
        }
        return null;
    }

    @Override
    public Map<String, Object> getCustomerServiceListByGameId(Long gameId) {
        QueryWrapper<CustomerService> wrapper = new QueryWrapper<CustomerService>()
                .in("game_id", gameId, 0)
                .eq("weight", 0);
        List<CustomerService> list = this.baseMapper.selectList(wrapper);
        Map<String, Object> map = new HashMap<>(1);
        map.put("list", list);
        return map;
    }
}
